package com.lanyam.travel.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanyam.travel.base.dao.PanicBuyMapper;
import com.lanyam.travel.base.entity.PanicBuy;
import com.lanyam.travel.base.entity.Product;
import com.lanyam.travel.base.entity.ScenicHotel;
import com.lanyam.travel.base.entity.SupplierInfo;
import com.lanyam.travel.base.entity.TravelLine;
import com.lanyam.travel.base.entity.User;
import com.lanyam.travel.base.entity.vo.PanicBuyVo;
import com.lanyam.travel.base.entity.vo.TravelOrderVo;
import com.lanyam.travel.base.service.IPanicBuyService;
import com.lanyam.travel.base.service.IScenicHotelService;
import com.lanyam.travel.base.service.ISupplierInfoService;
import com.lanyam.travel.base.service.ITravelLineService;
import com.lanyam.travel.base.service.ITravelOrderService;
import com.lanyam.travel.base.service.UserService;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rosson
 * @since 2020-01-27
 */
@Service
public class PanicBuyServiceImpl implements IPanicBuyService {

	@Autowired
	private PanicBuyMapper panicBuyMapper;
	
	@Autowired
	private ITravelOrderService travelOrderService;
	
	@Autowired
	private IScenicHotelService scenicHotelService;
	
	@Autowired
	private ITravelLineService travelLineService;
	
	@Autowired
	private ISupplierInfoService supplierInfoService;
	
	@Autowired
	private UserService userService;
	
	@Override
	public void addPanicBuy(PanicBuy panicBuy) {
		// TODO Auto-generated method stub
		panicBuyMapper.insert(panicBuy);
	}

	@Override
	public List<PanicBuyVo> getAllPanicBuy() {
		// TODO Auto-generated method stub
		List<PanicBuy> allPanicBuyList = panicBuyMapper.selectList(null);
		List<String> scenicIds = new ArrayList<String>();
		List<String> lineIds = new ArrayList<String>();
		for (PanicBuy panicBuy : allPanicBuyList) {
			if(panicBuy.getGoodType() > 1)
			{
				lineIds.add(panicBuy.getGoodsId());
			}
			else
			{
				scenicIds.add(panicBuy.getGoodsId());
			}
		}
		Map<String,Product> productMap = new HashMap<String, Product>();
		if(lineIds.size() > 0)
		{
			List<TravelLine> lines = travelLineService.batchQueryTravelLines(lineIds);
			for (TravelLine travelLine : lines) {
				productMap.put(travelLine.getId(), travelLine);
			}
		}
		if(scenicIds.size() > 0)
		{
			List<ScenicHotel> scenics = scenicHotelService.batchQueryScenics(scenicIds);
			for (ScenicHotel scenicHotel : scenics) {
				productMap.put(scenicHotel.getId(), scenicHotel);
			}
		}
		List<PanicBuyVo> returnList = new ArrayList<PanicBuyVo>();
		try {
			PanicBuyVo panicVo;
			for (PanicBuy panic : allPanicBuyList)
			{
				panicVo = new PanicBuyVo();
				BeanUtils.copyProperties(panicVo, panic);
				panicVo.setProduct(productMap.get(panic.getGoodsId()));
				returnList.add(panicVo);
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnList;
	}

	@Override
	public void updatePanicBuy(PanicBuy panicBuy) {
		// TODO Auto-generated method stub
		panicBuyMapper.updateById(panicBuy);
	}

	@Override
	public void deletePanicBuyById(String id) {
		// TODO Auto-generated method stub
		panicBuyMapper.deleteById(id);
	}

	@Override
	public PanicBuyVo queryPanicBuyInfoById(String id) {
		// TODO Auto-generated method stub
		try {
			PanicBuy panicBuy = panicBuyMapper.selectById(id);
			if(null != panicBuy)
			{
				PanicBuyVo panicVo = new PanicBuyVo();
				BeanUtils.copyProperties(panicVo, panicBuy);
				String productId = panicBuy.getGoodsId();
				int productType = panicBuy.getGoodType();
				Product product;
				if(productType > 1)
				{
					//查询线路
					product = travelLineService.queryTravelLineById(panicBuy.getGoodsId());
				}
				else
				{
					//查询景点酒店
					product = scenicHotelService.queryScenicById(panicBuy.getGoodsId());
				}
				panicVo.setContent(panicBuy.getContent());
				panicVo.setProduct(product);
				return panicVo;
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public PanicBuy queryPanicBuyById(String id) {
		// TODO Auto-generated method stub
		PanicBuy panicBuy = panicBuyMapper.selectById(id);
		if(null != panicBuy)
		{
			String productId = panicBuy.getGoodsId();
			int productType = panicBuy.getGoodType();
			Product product;
			if(productType > 1)
			{
				//查询线路
				product = travelLineService.queryTravelLineById(productId);
			}
			else
			{
				//查询景点酒店
				product = scenicHotelService.queryScenicById(productId);
			}
			panicBuy.setProduct(product);
		}
		return panicBuy;
	}

	@Override
	public void updatePanicBuyStatus(String id, int status) {
		// TODO Auto-generated method stub
		panicBuyMapper.updatePanicBuyStatus(id,status);
	}

	@Override
	public List<Product> queryProductByType(int productType) {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> scenicWrapper = new QueryWrapper<ScenicHotel>();
		QueryWrapper<TravelLine> lineWrapper = new QueryWrapper<TravelLine>();
		List<Product> list;
		if(productType == 0)
		{
			scenicWrapper.eq("product_type", 0);
			list = (List<Product>) scenicHotelService.selectList(scenicWrapper);
		}
		else if(productType == 1)
		{
			scenicWrapper.eq("product_type", 1);
			list = (List<Product>)scenicHotelService.selectList(scenicWrapper);
		}
		else if(productType == 2)
		{
			//国外线路
			lineWrapper.eq("region", 1);
			list = (List<Product>)travelLineService.selectList(lineWrapper);
		}
		else if(productType == 3)
		{
			//国内线路
			lineWrapper.eq("region", 0);
			list = (List<Product>)travelLineService.selectList(lineWrapper);
		}
		else
		{
			list = new ArrayList<Product>();
		}
		return list;
		
	}

	@Override
	public Page<PanicBuyVo> getPanicBuyPage(int pageNum, int pageSize, Map<String,Object> reqPara) {
		// TODO Auto-generated method stub
		Page<PanicBuyVo> page = new Page<PanicBuyVo>(pageNum, pageSize);
		Map<String,Object> map = new HashMap<String, Object>();
		String goodType = reqPara.get("goodType")!=null?reqPara.get("goodType").toString():"";
		if(!StringUtils.isEmpty(goodType))
		{
			map.put("goodType", goodType);
		}
		String status = reqPara.get("status")!=null?reqPara.get("status").toString():"";
		if(!StringUtils.isEmpty(status))
		{
			map.put("status", status);
		}
		String goodsName = reqPara.get("name")!=null?reqPara.get("name").toString():"";
		if(!StringUtils.isEmpty(goodsName))
		{
			map.put("goodsName", goodsName);
		}
		String supplierId = reqPara.get("supplierId")!=null?reqPara.get("supplierId").toString():"";
		if(!StringUtils.isEmpty(supplierId))
		{
			map.put("supplierId", supplierId);
		}
		try {
			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd");
			String startDate = reqPara.get("startDate")!=null?reqPara.get("startDate").toString():"";
			if(!StringUtils.isEmpty(startDate))
			{
				Date startTime = simpleDateFormat.parse(startDate);
				map.put("startTime", startTime);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(startTime);
				calendar.add(Calendar.DAY_OF_YEAR, 1);
				Date endTime = calendar.getTime();
				map.put("endTime", endTime);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		Page<PanicBuyVo> pageInfo = panicBuyMapper.getPanicBuyPage(page, map);
		return pageInfo;
	}

	@Override
	public List<Product> getProductByUserAndType(String userId, int productType) {
		// TODO Auto-generated method stub
		User loginUser = userService.queryUserById(userId);
		QueryWrapper<ScenicHotel> scenicWrapper = new QueryWrapper<ScenicHotel>();
		QueryWrapper<TravelLine> lineWrapper = new QueryWrapper<TravelLine>();
		if(!StringUtils.isEmpty(loginUser.getSupplyId()))
		{
			scenicWrapper.eq("supply_id", loginUser.getSupplyId());
			lineWrapper.eq("supply_id", loginUser.getSupplyId());
		}
		List<Product> list;
		if(productType == 0)
		{
			scenicWrapper.eq("product_type", 0);
			list = (List<Product>) scenicHotelService.selectList(scenicWrapper);
		}
		else if(productType == 1)
		{
			scenicWrapper.eq("product_type", 1);
			list = (List<Product>)scenicHotelService.selectList(scenicWrapper);
		}
		else if(productType == 2)
		{
			//国外线路
			lineWrapper.eq("region", 1);
			list = (List<Product>)travelLineService.selectList(lineWrapper);
		}
		else if(productType == 3)
		{
			//国内线路
			lineWrapper.eq("region", 0);
			list = (List<Product>)travelLineService.selectList(lineWrapper);
		}
		else
		{
			list = new ArrayList<Product>();
		}
		return list;
	}
}
